﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringHelper.cs" company="Collaboris Ltd">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the StringHelper type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Utils
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Xml;

    /// <summary>
    /// Helper class for managing strings.
    /// </summary>
    public class StringHelper
    {
        /// <summary>
        /// Converts the stream to string.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns></returns>
        public static string ConvertStreamToString(Stream stream)
        {
            //stream.Position = 0;
            using (StreamReader Reader = new StreamReader(stream))
                return Reader.ReadToEnd();
        }

        /// <summary>
        /// Converts the string passed to a memory stream.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns></returns>
        public static MemoryStream ConvertStringToMemoryStream(string s)
        {
            ASCIIEncoding myEncoder = new ASCIIEncoding();
            Byte[] bytes = myEncoder.GetBytes(s);
            MemoryStream ms = new MemoryStream(bytes);
            return ms;

        }

        public static string ConvertToBoolString(string val)
        {
            if (val.ToLower() == "true" || val.ToLower() == "1" || val.ToLower() == "yes")
                return "true";
            else
                return "false";

        }

        public static string ConvertToBoolString(bool val)
        {
            if (val)
                return "true";
            else
                return "false";

        }




        /// <summary>
        /// Adds spaces to a SentenceCase string
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns></returns>
        public static string SpaceMeUp(string s)
        {
            string sBuf = "";
            for (int i = 0; i < s.Length; i++)
            {
                if (i == 0)
                {
                    sBuf += s.Substring(i, 1);
                }
                else
                {
                    if (s.Substring(i, 1) == s.Substring(i, 1).ToUpper())
                    {
                        sBuf += " " + s.Substring(i, 1);
                    }
                    else
                    {
                        sBuf += s.Substring(i, 1);
                    }
                }
            }

            return sBuf;
        }
        /// <summary>
        /// Removes the "@" sign if its in first position.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns></returns>
        public static string RemoveAtSign(string s)
        {
            if (s[0].ToString() == "@")
            {
                return s.Substring(1);
            }
            else
            {
                return s;
            }
        }

        /// <summary>
        /// Escapers the Curly Brackets
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string EscapeForFormat(string input)
        {
            input = input.Replace("{", @"{{");
            input = input.Replace("}", @"}}");
            return input;
        }

        private static bool ContainsReservedCharacters(string unescapedString)
        {
            return (-1 != unescapedString.IndexOfAny(charsToEscape));
        }

        private static char[] charsToEscape = new char[] { '%', '*', '?', '@', '$', '(', ')', ';', '\'' };

        /// <summary>
        /// Escapes the specified unescaped string.
        /// </summary>
        /// <param name="unescapedString">The unescaped string.</param>
        /// <returns></returns>
        public static string Escape(string unescapedString)
        {
            ValidationHelper.VerifyStringArgument(unescapedString, "unescapedString");
            if (!ContainsReservedCharacters(unescapedString))
            {
                return unescapedString;
            }
            StringBuilder builder = new StringBuilder(unescapedString);
            foreach (char ch in charsToEscape)
            {
                int num = Convert.ToInt32(ch);
                string newValue = string.Format(CultureInfo.InvariantCulture, "%{0:x00}", new object[] { num });
                builder.Replace(ch.ToString(CultureInfo.InvariantCulture), newValue);
            }
            return builder.ToString();
        }

        /// <summary>
        /// Formats and indents the XML.
        /// </summary>
        /// <param name="XML">The XML.</param>
        /// <returns></returns>
        public static string FormatXml(string XML)
        {
            string xml = string.Empty;

            MemoryStream memoryStream = new MemoryStream();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.Unicode);
            XmlDocument xmlDocument = new XmlDocument();

            try
            {

                // Load the XmlDocument with the XML.
                xmlDocument.LoadXml(XML);

                xmlTextWriter.Formatting = Formatting.Indented;

                // Write the XML into a formatting XmlTextWriter
                xmlDocument.WriteContentTo(xmlTextWriter);
                xmlTextWriter.Flush();
                memoryStream.Flush();

                // Have to rewind the MemoryStream in order to read
                // its contents.
                memoryStream.Position = 0;

                // Read MemoryStream contents into a StreamReader.
                StreamReader streamReader = new StreamReader(memoryStream);

                // Extract the text from the StreamReader.
                string formattedXml1 = streamReader.ReadToEnd();

                xml = formattedXml1;
            }
            catch (XmlException exception)
            {

            }

            memoryStream.Close();
            xmlTextWriter.Close();

            return xml;
        }

       

    }
}